import {
  wxRequest,
  isOnline
} from './utils/wxRequest.js'
import Orders from './utils/Orders.js'
import {
  wxLogin,
  loginwithCode
} from './utils/user'

App({
  // state
  globalData: {
    openid: '',
    authinfo: null,
    isLogin: false,
    userinfo: null
  },
  PCOLOR: "#ffa010",
  _http: wxRequest,
  order: new Orders(),
  isShowTotal: false,
  mockDatas: [],
  mockFoodsDetail: [],
  shopName: '',
  store: '',

  /************************************************************************ 生命周期 *********************************************************/
  async onLaunch() {
    // 登录
    await this.applogin()

    this.isOnline = isOnline
    let isLogin = wx.getStorageSync('isLogin')
    this.globalData.isLogin = isLogin
    // await this.initMockShow()

  },
  /************************************************************************ 生命周期end *********************************************************/


  /************************************************************************ 自定义全局方法 *********************************************************/
  async applogin() {
    const res = await wxLogin()
    if (res.errMsg == "login:ok") {
      console.log(res, 'res')
      const res2 = await loginwithCode(res.code)
      console.log(res2, 'res2');
      
      this.globalData.openid = res2.data.openid
    }

  },
  judgeAuth() {
    return new Promise(resolve => {
      wx.getSetting({
        success: (res) => {
          if (res.authSetting['scope.userInfo']) {
            resolve({
              msg: '已授权用户信息',
              code: 111
            })
          } else {
            resolve({
              msg: '未授权用户信息',
              code: 222
            })
          }
        },
        fail: () => {
          resolve({
            msg: '未授权用户信息',
            code: 222
          })
        }
      })
    })
  },
  async judgeAuthAndLogin() {
    const res1 = await this.judgeAuth()
    if (res1.code !== 111) {
      return wx.showModal({
        title: '提示',
        content: '请前往授权',
        confirmColor: this.PCOLOR,
        success: result => {
          if (result.confirm) {
            wx.navigateTo({
              url: '/pages/auth/auth'
            })
          }
        }
      })
    } else {
      this.globalData.isLogin = true
    }
    const res2 = this.judgeLogin()
    if (!res2) {
      return wx.showModal({
        title: '提示',
        content: '请登录',
        confirmColor: this.PCOLOR,
        success: res => {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/mine/mine'
            })
          }
        }
      })
    } else {
      this.globalData.isLogin = true
    }
    return true
  },
  judgeLogin() {
    if (this.globalData.isLogin) {
      return true
    } else {
      return false
    }
  },
  // 根据getuserinfo 的 e 参数， 设置缓存和global
  setStorageAndGlobal(e) {
    let authinfo = JSON.parse(e.rawData)
    this.globalData.isLogin = true
    this.globalData.authinfo = authinfo
    wx.setStorageSync('authinfo', authinfo)
  },
  async initMockShow() {
    const res = await this._http({
      url: '/system/getstatus',
      method: 'post',
      data: {
        key: 'JHIJfdswqefsdDfro21gmdsl2',
        user: 'glack'
      }
    })
    console.log(res, 'res');

    if (res.data.status === 'online') {
      this.isShowTotal = true
    }else{
      this.isShowTotal = false
    }
  }
  /************************************************************************ 自定义全局方法end *********************************************************/

})